package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.atguigu.ssyx.mq.result.Result;
import com.atguigu.ssyx.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PermissionController
 * Package: com.atguigu.ssyx.acl.controller
 * Description:
 *
 * @Author Root
 * @Create 2023/9/7 14:14
 * @Version 1.0
 */
@RestController
//@CrossOrigin
@Api(tags = "菜单管理")
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @ApiOperation("为角色分配菜单")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long roleId,
                           @RequestParam Long[] permissionId){
        rolePermissionService.doAssignByRoleId(roleId,permissionId);
        return Result.ok(null);
    }

    @ApiOperation("获取所有菜单和已分配的菜单")
    @GetMapping("toAssign/{roleId}")
    public Result doAssign(@PathVariable Long roleId){
        Map<String, Object> map = new HashMap<>();
        map=permissionService.getPermissionByRoleId(roleId);
        return Result.ok(map.get("allPermissions"));
    }

    //查询所有菜单
    @GetMapping
    @ApiOperation("查询所有菜单")
    public Result list() {
        List<Permission> list = permissionService.queryAllPermission();
        return Result.ok(list);
    }

    //添加菜单
    @ApiOperation("添加菜单")
    @PostMapping("save")
    public Result save(@RequestBody Permission permission) {
        boolean is_success = permissionService.save(permission);
        if (is_success) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    //修改菜单
    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result update(@RequestBody Permission permission){
        boolean is_success = permissionService.updateById(permission);
        if (is_success) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }
    //递归删除菜单
    @ApiOperation("递归删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
       permissionService.removeChileById(id);
       return Result.ok(null);
    }

}
